package com.qf.security.handlers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.dto.R;
import com.qf.exeptions.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
杨威
2022/1/7
22:17
*/
@Component
public class MyLoginFailureHandlder implements AuthenticationFailureHandler {

    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        R r =R.setError(ResultCode.USERNAME_PASSOERD_ERROR,"用户名或者密码错误");
        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(r));
    }
}
